package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.service.IUserinfoRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.Userinfo;
import cn.wolfcode.luowowo.member.service.IUserinfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserinfoService userinfoService;

    @Reference
    private IUserinfoRedisService redisService;

    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin (String phone, String password, HttpServletResponse response) {
        Userinfo userinfo = userinfoService.loginIn(phone, password);

        //1.放到redis里。并定时
        String newKey=redisService.getKeyByPuttingUserinfoIntoRedis(phone,userinfo);
        //2. 放到cookie  里面
        Cookie cookie=new Cookie("token",newKey);
        cookie.setMaxAge(RedisKeys.USERINFO_TOKEN.getTimeout());
        cookie.setPath("/");
        response.addCookie(cookie);
        return AjaxResult.SUCCESS;
    }
}
